﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ResAddApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string dataFromClient = Server.UrlDecode(Request.Form.ToString());
        if (dataFromClient.Length == 0)
        {
            Response.Write("error 0: " + dataFromClient);
            return;
        }
        if (Request.Cookies["username"] == null && Request.Url.ToString().IndexOf("localhost") == -1)
        {
            if (Request.QueryString != null && Request.QueryString["paraStr"] != null)
            {
                string paraStr = Request.QueryString["paraStr"];
                string file = paraStr.Substring(1, paraStr.Length - 1);
                Request.Cookies.Add(new HttpCookie("username", file));
            }
            else
            {
                Response.Write("error 1: " + Request.Cookies["username"]);
                return;
            }
            
        }
        if (Request.Url.ToString().IndexOf("localhost") > -1)
        {
            Request.Cookies.Add(new HttpCookie("username", "localhost"));
        }
        string domain = URL.getDomain(Request.Url.ToString());
        string domainSuf = URL.getDomainSuf(domain);

        string directory = "";
        if (Request.QueryString != null && Request.QueryString["flag"] != null)
        {
            directory = Request.QueryString["flag"];
        }
        try
        {
            int result = handleDataFromClient.handle(dataFromClient, Request.Cookies["username"].Value, directory);//need to be modified.
            if (result == 1)
            {
                Response.Write("successed;");
            }
            else
            {
                Response.Write("failed;");
            }

        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            Response.Write(ex.ToString());
        }
    }
}